/**
 * @{#} InitManager.java Create on 2014年11月13日 下午2:52:53    
 *
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0
 * @description
 *
 */
package com.common.manager.init;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;

import com.common.manager.image.OldRoundedBitmapDisplayer;
import com.common.utility.CommonUtility;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;


public class InitManager {

    private static InitManager manager;

    private InitManager() {
    }

    public static InitManager getInstance() {
        if (CommonUtility.Utility.isNull(manager)) {
            manager = new InitManager();
        }
        return manager;
    }

    /**
     * 初始化ImageLoader配置
     *
     * @param context
     */
    public void initImageLoaderConfig(Context context) {
        DisplayImageOptions options = getOptions(0);
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                context)
                .defaultDisplayImageOptions(options)
                .memoryCacheSize(10 * 1024 * 1024)
                .diskCacheSize(50 * 1024 * 1024)
                .memoryCache(new WeakMemoryCache())
                .imageDownloader(
                        new BaseImageDownloader(context, 30 * 1000, 30 * 1000))
                        // connectTimeout
                        // (5
                        // s),
                        // readTimeout
                        // (30
                        // s)超时时间
                .diskCacheFileCount(100)// 缓存一百张图片
                .writeDebugLogs().build();
        ImageLoader.getInstance().init(config);
    }

	/*
	 * (non-Javadoc)
	 *
	 * @see java.lang.Object#clone()
	 */

    public DisplayImageOptions getOptions(int roundSize) {
        return getOptionBuilder(roundSize).build();
    }

    public DisplayImageOptions.Builder getOptionBuilder(int roundSize) {
        DisplayImageOptions.Builder builder = new DisplayImageOptions.Builder()
                .cacheInMemory(false).cacheOnDisk(true)
                .considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565);
        if (roundSize > 0) {
            builder.displayer(new OldRoundedBitmapDisplayer(roundSize));
        }
        return builder;

    }

    public DisplayImageOptions getOptions(int roundSize,
                                          Drawable emptyDrawable, Drawable loadingDrawable,
                                          Drawable failDrawable) {
        DisplayImageOptions.Builder builder = new DisplayImageOptions.Builder()
                .cacheInMemory(false).cacheOnDisk(true)
                .considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565)
                .showImageOnFail(failDrawable)
                .showImageOnLoading(loadingDrawable)
                .showImageForEmptyUri(emptyDrawable);
        if (roundSize > 0) {
            builder.displayer(new OldRoundedBitmapDisplayer(roundSize));
        }
        DisplayImageOptions options = builder.build();
        return options;
    }

    /**
     *
     * @param roundSize
     * @param loadingImage
     * @param loadingFail
     * @param emptyPic
     * @return
     */
    public DisplayImageOptions getOptions(int roundSize, int loadingImage,
                                          int loadingFail, int emptyPic) {
        DisplayImageOptions.Builder builder = new DisplayImageOptions.Builder()
                .cacheInMemory(false).cacheOnDisk(true)
                .considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565)
                .showImageOnLoading(loadingImage)
                .showImageForEmptyUri(emptyPic).showImageOnFail(loadingFail);
        if (roundSize > 0) {
            builder.displayer(new OldRoundedBitmapDisplayer(roundSize));
        }
        DisplayImageOptions options = builder.build();
        return options;
    }
}
